﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.Utils;
using WebApiTest.Service.System;

namespace WebApiTest.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private AccountService accountService = new AccountService();

        [HttpPost("login")]
        public JObject login(JObject request)
        {
            JObject jObject = new JObject();
            try
            {
                jObject = accountService.loginCheck(request);
                if(jObject["status"].ToString().Equals("200"))
                {
                    this.Request.Headers.Add("token_status", "ok");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return jObject;
        }

        [HttpPost("info")]
        public ActionResult<OpResultDTO> info()
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                string token = this.Request.Headers["Access-Token"];
                opResult.message = "seccess";
                opResult.result = accountService.getInfo(token);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }
    }
}
